
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.contrib.auth.hashers import check_password,make_password
from .models import *
from RBAC.serializers import *
# Create your views here.


# 注册

class RegisterView(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')

        user_info = UserModel.objects.filter(username=username,password=password).first()

        if user_info:
            return Response({'code':400,'msg':'账号已存在'})



        #  密码加密
        make_pwd = make_password(password)
        #  抛出个try异常
        try:
            UserModel.objects.create(
                username=username,
                password=make_pwd
            )
        except Exception as e:
            print('注册报错>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'注册失败'
            })
        return Response({
            'code':200,
            'msg':'注册成功'
        })


#  登录
class LoginView(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')

        user_info = UserModel.objects.filter(username=username).first()

        if not user_info:
            return Response({
                'code':400,
                'msg':'用户名错误'
            })
        check_pwd = check_password(password, user_info.password)
        print('????????????????????????????？》》》》》》》》',user_info.password)
        print('????????????????????????????？》》》》》》》》',user_info.username)
        print('????????????????????????????？》》》》》》》》',type(user_info.username))



        if check_pwd:

            return Response({
                'code': 200,
                'msg': '登录成功'
            })
        else:
            return Response({
                'code': 400,
                'msg': '登录失败,密码错误'
            })


# 展示用户信息


class ShowUserView(APIView):
    def get(self,request):
        data = UserModel.objects.all()
        list = []
        for i in data:
            aa = {
                'id':i.id,
                'username':i.username,
                'position':i.position,
                'role':i.role,
                'permission':i.permission,
                'branch':i.branch,
                'rank':i.rank,
            }
            list.append(aa)
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':list
        })


#  添加用户信息


class CreateUserView(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        position = request.data.get('position')
        role = request.data.get('role')
        permission = request.data.get('permission')
        branch = request.data.get('branch')
        rank = request.data.get('rank')

        try:
            UserModel.objects.create(
                username=username,
                password=password,
                position=position,
                role=role,
                permission=permission,
                branch=branch,
                rank=rank
            )
        except Exception as e:
            print('添加错误>>>>>>>>',e)
            return Response({
                'code': 400,
                'msg':'添加失败',
            })
        return Response({
            'code':200,
            'msg':'添加成功',
        })


# 修改用户信息


class UpdataView(APIView):
    def put(self,request, id):
        username = request.data.get('username')
        password = request.data.get('password')
        position = request.data.get('position')
        role = request.data.get('role')
        permission = request.data.get('permission')
        branch = request.data.get('branch')
        rank = request.data.get('rank')

        try:
            UserModel.objects.filter(id=id).update(
                username=username,
                password=password,
                position=position,
                role=role,
                permission=permission,
                branch=branch,
                rank=rank
            )
            return Response({"code": 200, "msg": "修改成功"})
        except Exception as e:
            print(e)
            return Response({"code": 400, "msg": "修改失败"})


# 删除用户信息


class DeleteUserView(APIView):
    def delete(self,request,id):
        try:
            UserModel.objects.filter(id=id).delete()
            return Response({"code": 200, "msg": "删除成功"})
        except Exception as e:
            print(e)
            return Response({"code": 400, "msg": "删除失败"})


# 展示权限

class ShowPermissionView(APIView):
    def get(self,request):
        data = PermissionModel.objects.all()
        ser = Permissionser(data,many=True)
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':ser.data,
        })

# 权限的添加


class CreatePermissionView(APIView):
    def post(self,request):
        permission_name = request.data.get('permission_name')
        permission_level = request.data.get('permission_level')
        permission_url = request.data.get('permission_url')
        beizhu = request.data.get('beizhu')


        try:
            PermissionModel.objects.create(
                permission_name=permission_name,
                permission_level=permission_level,
                permission_url=permission_url,
                beizhu=beizhu,

            )
        except Exception as e:
            print('添加权限错误的原因>>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'权限添加失败'
            })
        return Response({
            'code':200,
            'msg':'权限添加成功'
        })

#  权限的修改


class UpdatePermissionView(APIView):
    def put(self,request,id):
        permission_name = request.data.get('permission_name')
        permission_level = request.data.get('permission_level')
        permission_url = request.data.get('permission_url')
        beizhu = request.data.get('beizhu')


        try:
            PermissionModel.objects.filter(id=id).update(
                permission_name=permission_name,
                permission_level=permission_level,
                permission_url=permission_url,
                beizhu=beizhu,

            )
        except Exception as e:
            print('修改错误的原因>>>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'修改失败'
            })
        return Response({
            'code':200,
            'msg':'修改成功'
        })


#  权限删除


class DeletePermissionView(APIView):
    def delete(self,request,id):
        try:
            PermissionModel.objects.filter(id=id).delete()
        except Exception as e:
            print('删除失败的原因>>>>>',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code': 200,
            'msg': '删除成功'
        })

# 查看所有部门信息


class ShowBranchView(APIView):
    def get(self,request):
        data = BranchModel.objects.all()
        ser = Branchser(data,many=True)
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':ser.data
        })


# 部门添加
class CreateBranchView(APIView):
    def post(self, request):
        branch_name = request.data.get('branch_name')
        beizhu = request.data.get('beizhu')
        try:

            BranchModel.objects.create(
                branch_name=branch_name,
                beizhu=beizhu

            )
        except Exception as e:
            print('>>>>>>>>>>>>>>>>>>>>添加失败的原因',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })
        return Response({
            'code':200,
            'msg':'添加成功'
        })


#         部门修改

class UpdateBranchView(APIView):
    def put(self,request,id):
        branch_name = request.data.get('branch_name')
        beizhu = request.data.get('beizhu')
        try:
            BranchModel.objects.filter(
                id=id

            ).update(
                branch_name=branch_name,
                beizhu=beizhu,
            )
        except Exception as e:
            print('修改失败的原因>>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'修改失败'
            })
        return Response({
            'code':200,
            'msg':'修改成功'
        })
# 部门删除


class DeleteBranchView(APIView):
    def delete(self,request,id):
        try:
            BranchModel.objects.filter(id=id).delete()
        except Exception as e:
            print('删除失败的原因>>>>>',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code': 200,
            'msg': '删除成功'
        })

# 展示所有的职级信息


class ShowRankView(APIView):
    def get(self,request):
        data = RankModel.objects.all()
        ser = Rankser(data,many=True)

        return Response({
            'code':200,
            'msg':'展示成功',
            'data': ser.data
        })

#  新增一个职级信息

class CreateRankView(APIView):
    def post(self,request):
        rank_name = request.data.get('rank_name')
        beizhu = request.data.get('beizhu')
        try:
            RankModel.objects.create(
                rank_name=rank_name,
                beizhu = beizhu,
            )
        except Exception as e:
            print('添加失败的原因》》》》》》》',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })
        return Response({
            'code':200,
            'msg':'添加成功'
        })

# 修改职级


class UpdateRankView(APIView):
    def put(self,request,id):
        rank_name = request.data.get('rank_name')
        beizhu = request.data.get('beizhu')
        try:
            RankModel.objects.filter(id=id).update(
                rank_name=rank_name,
                beizhu=beizhu,
            )
        except Exception as e:
            print('修改失败的原因>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'修改失败'
            })
        return Response({
            'code':200,
            'msg':'修改成功'
        })


# 删除职级
class DeleteRankView(APIView):
    def delete(self,request,id):
        try:
            RankModel.objects.filter(id=id).delete()
        except Exception as e:
            print('删除失败的原因>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'删除失败',
            })
        return Response({
            'code':200,
            'msg':'删除成功',
        })




#  ★★★☆★

# 展示角色信息


class ShowRoleView(APIView):
    def get(self,request):
        data = RoleModel.objects.all()
        ser = Roleser(data,many=True)

        return Response({
            'code':200,
            'msg':'展示成功',
            'data': ser.data
        })

#  新增一个角色信息


class CreateRoleView(APIView):
    def post(self,request):
        role_name = request.data.get('role_name')
        beizhu = request.data.get('beizhu')
        try:
            RoleModel.objects.create(
                role_name=role_name,
                beizhu=beizhu,
            )
        except Exception as e:
            print('添加失败的原因》》》》》》》',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })

        return Response({
            'code':200,
            'msg':'添加成功'
        })

# 修改角色信息


class UpdateRoleView(APIView):
    def put(self,request, id):
        role_name = request.data.get('role_name')
        beizhu = request.data.get('beizhu')
        try:
            RoleModel.objects.filter(id=id).update(
                role_name=role_name,
                beizhu=beizhu
            )
        except Exception as e:
            print('修改失败的原因>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'修改失败'
            })
        return Response({
            'code':200,
            'msg':'修改成功'
        })

# 删除角色信息


class DeleteRoleView(APIView):
    def delete(self,request,id):
        try:
            RoleModel.objects.filter(id=id).delete()
        except Exception as e:
            print('删除失败的原因>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'删除失败',
            })
        return Response({
            'code':200,
            'msg':'删除成功',
        })

# 展示岗位信息


class ShowPositionView(APIView):
    def get(self,request):
        data = PositionModel.objects.all()
        ser = Positionser(data,many=True)
        return Response({
            'code':200,
            'msg':'成功',
            'data':ser.data,
        })


# 添加岗位


class CreatePositionView(APIView):
    def post(self,request):
        position_name = request.data.get('position_name')
        beizhu = request.data.get('beizhu')

        try:
            PositionModel.objects.create(
                position_name=position_name,
                beizhu=beizhu,

            )
        except Exception as e:
            print('>>>>>>>>>>>>>>>>>>>>>>>>>添加岗位失败的原因',e)
            return Response({'code':400,'msg':'添加失败'})
        return Response({
            'code':200,
            'msg':'添加成功'
        })
# 修改岗位
class UpdatePositionView(APIView):
    def put(self,request,id):
        position_name = request.data.get('position_name')
        beizhu = request.data.get('beizhu')

        try:
            PositionModel.objects.filter(id=id).update(
                position_name=position_name,
                beizhu=beizhu
            )
        except Exception as e:
            print('>>>>>>>>>>>>修改失败的原因')
            return Response({
                'code':400,
                'msg':'修改失败'
            })
        return Response({
            'code':200,
            'msg':'修改成功'
        })

class DeletePositionView(APIView):
    def delete(self,request,id):
        try:
            PositionModel.objects.filter(id=id).delete()
        except Exception as e:
            print('删除岗位失败的原因>>>>>>>>>>>>',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code':200,
            'msg':'删除成功！'
        })

# 展示菜单信息


class ShowMenuView(APIView):
    def get(self,request):
        data = MenuModel.objects.all()
        ser = Menuser(data,many=True)

        return Response({
            'code':200,
            'msg':'获取成功',
            'data':ser.data,
        })

# 添加菜单信息


class CreateMenuView(APIView):
    def post(self,request,):
        menu_name = request.data.get('menu_name')
        address = request.data.get('address')
        parent = request.data.get('parent')


        try:
            MenuModel.objects.create(
                menu_name=menu_name,
                address=address,
                parent=parent,

            )
        except Exception as e:
            print('>>>>>>>>>>>>>>>>添加失败的原因',e)
            return Response({
                'code':400,
                'msg':'添加失败',

            })
        return Response({
            'code':200,
            'msg':'添加成功'
        })

# 修改菜单信息


class UpdateMenuView(APIView):
    def put(self,request,id):
        menu_name = request.data.get('menu_name')
        address = request.data.get('address')
        parent = request.data.get('parent')


        try:
            MenuModel.objects.filter(id=id).update(
                menu_name=menu_name,
                address=address,
                parent=parent,
            )
        except Exception as e:
            print('>>>>>>修改失败的原因',e)
            return Response({
                'code':400,
                'msg':'修改失败',
            })
        return Response({
            'code':200,
            'msg':'修改成功'
        })


# 删除菜单信息

class DeleteMenuView(APIView):
    def delete(self,request,id):
        try:
            MenuModel.objects.filter(id=id).delete()
        except Exception as e:
            print('>>>>删除失败的原因>>>>>>',)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code':200,
            'msg':'删除成功',
        })

class ShowMoneyBomView(APIView):
    def get(self,request):
        data =MoneyBomModel.objects.all()

        list = []

        for i in data:
            aa = {
                'id':i.id,
                'user':i.user.username,
                'work_day':i.work_day,
                'absenteeism_day':i.absenteeism_day,
                'rest_day':i.rest_day,
                'basic_money':i.basic_money,
                'rank_money':i.rank_money,
            }
            list.append(aa)
        return Response({
            'code':200,
            'msg':'成功',
            'data':list
        })


class ShowVehicleModelView(APIView):
    def get(self,request):
        data = VehicleModel.objects.all()

        list = []

        for i in data:
            aa ={
                'id':i.id,
                'vehicle_type':i.vehicle_type,
                'vehicle_number':i.vehicle_number,
                'vehicle_state':i.vehicle_state,
            }
            list.append(aa)
        return Response({
            'code':200,
            'msg':'成功',
            'data':list
        })

